<?php
namespace app\web\model;


use app\lib\exception\BaseException;

/**
 * 技师
 * @package app\basic\model
 */
class Technician extends Common 
{
    protected function getLocationAttr($v,$data)
    {
        if(!$v) return '';
        return '<a target="_blank" href="https://apis.map.qq.com/tools/poimarker?type=0&marker=coord:'.$data['latitude'].",".$data['longitude'].';&key='.config('api.wx_map_key').'&referer=myapp">最后一次打卡地</a>';
    }
    protected function getPeriodTimeAttr($v)
    {
        return $v == 0 ? '无限期' : date('Y-m-d H:i',$v);
    }
    protected function getSubTimeAttr($v)
    {
        return $v ? date('Y-m-d',$v) : '';
    }
    protected function getCheckTimeAttr($v)
    {
        return $v ? date('Y-m-d',$v) : '';
    }
    protected function getAlbumSubTimeAttr($v)
    {
        return $v ? date('Y-m-d',$v) : '';
    }
    protected function getWorksSubTimeAttr($v)
    {
        return $v ? date('Y-m-d',$v) : '';
    }
    protected function getAlbumAttr($v)
    {
        return $v ? explode(',', $v) : [];
    }
    protected function setAlbumAttr($v)
    {
        return is_array($v) ? implode(',',$v) : $v;
    }
    protected function setWorksAttr($v)
    {
        return is_array($v) ? implode(',',$v) : $v;
    }
    protected function getWorksAttr($v)
    {
        return $v ? explode(',', $v) : [];
    }
    protected function getAlbumCheckTimeAttr($v){
        return $v ? date('Y-m-d',$v) : '';
    }
    protected function getWorksCheckTimeAttr($v){
        return $v ? date('Y-m-d',$v) : '';
    }
    protected function getShopCheckTimeAttr($v){
        return $v ? date('Y-m-d',$v) : '';
    }
    protected function setCityAttr($v,$data)
    {
        if($v == '市辖区' || $v == '县') return $data['province'];
        return $v;
    }
    public static function getAuthentication($uid){
        $info = self::where('user_id',$uid)
                    ->field('id,id_card,shop_id,shop_code,qualifications_status,album_status,works_status')
                    ->find()
                    ->toArray();
        $list = TechnicianCheck::where(['technician_id'=>$info['id']])
                        ->field('status,field_key,field_val,no_check_desc,check_time')
                        ->all()
                        ->toArray();
        $list = array_column($list,null,'field_key');
        $arr['is_real'] = isset($list['id_card']) ? $list['id_card']['status'] : -1;   //是否实名认证  -1 未提交资料
        $arr['is_avatar_url'] = isset($list['avatarUrl']) ? $list['avatarUrl']['status'] : -1;   //头像认证 -1未上传
        $arr['qualifications_status'] = $info['qualifications_status'];      //资质认证状态-1未提交0待审核1已认证2未通过
        $arr['is_promise_path'] = isset($list['promise_path']) ? $list['promise_path']['status'] : -1;
        $arr['is_job'] = isset($list['job_ids']) ? $list['job_ids']['status'] : -1;    //职称认证-1未提交资料0待审核1已认证2未通过认证
        $arr['is_shop'] = $info['shop_id'] == 0 ? 0 : 1;                       //是否加入商户
        $arr['is_album'] = $info['album_status'];   //个人相册是否通过 0未提交1待审核2已审核3未通过
        $protected = Project::where([['technician_id','=',$info['id']],['all_status','<>',1]])->find();
        $a = Project::where(['technician_id'=>$info['id']])->find();
        $arr['is_project'] = !$a ? -1 : (!empty($protected['id']) ? $protected['id'] : 0);
        $arr['is_works'] = $info['works_status'];   //作品相册是否通过0未提交1待审核2已审核3未通过
        return $arr;
        // $info['is_real'] = $info['id_card'] ? 1 : 0;                            //是否实名

        // $info['is_shop'] = $info['shop_id'] == 0 ? 0 : 1;                       //是否加入商户
        // $info['promise_path'] = $info['promise_path'] ? 1 : 0;                     //是否上传承诺函
        // $info['avatar_similarity'] = $info['avatar_similarity'] > 65 ? 1 : 0;   //是否头像认证
        // // $info['album_status'] = $info['album_status'] == 2 ? 1 : 0;             //个人相册是否通过
        // // $info['works_status'] = $info['works_status'] == 2 ? 1 : 0;             //作品相册是否通过
        
        // $info['project_id'] = Project::where([['technician_id','=',$info['id']],['all_status','<>',1]])->find();
        // $info['project_id'] = $info['project_id'] ? $info['project_id']['id'] : 0;
        // return $info; 
    }


    public static function getCheckMessage($t_id)
    {
        $info = self::get($t_id);
        $list = TechnicianCheck::where(['technician_id'=>$info->id])->field('field_key,field_val,status,no_check_desc')->all()->toArray();
        $list = array_column($list,null,'field_key');
        $arr = [];
        if(isset($list['id_card'])){
            if($list['id_card']['status'] == 2) $arr[] = '实名认证未通过:'.$list['id_card']['no_check_desc'];
        }else{
            $arr[] = '未实名认证,请尽快提交资料';
        }
        if(isset($list['avatarUrl'])){
            if($list['avatarUrl']['status'] == 2) $arr[] = '头像认证未通过:'.$list['avatarUrl']['no_check_desc'];
        }else{
            $arr[] = '未头像认证,请尽快提交资料';
        }
        if(isset($list['promise_path'])){
            if($list['promise_path']['status'] == 2) $arr[] = '承诺函未通过:'.$list['promise_path']['no_check_desc'];
        }else{
            $arr[] = '未上传承诺函,请尽快上传资料';
        }
        if(isset($list['job_ids'])){
            if($list['job_ids']['status'] == 2) $arr[] = '职称认证未通过:'.$list['job_ids']['no_check_desc'];
        }
        if(isset($list['technician_introduction'])){
            if($list['technician_introduction']['status'] == 2) $arr[] = '个人介绍未通过:'.$list['technician_introduction']['no_check_desc'];
        }
        if($info['works_status'] == 3){
            $arr[] = '作品相册审核未通过'.$info['works_reason'];
        } 
        if($info['album_status'] == 3){
            $arr[] = '技师相册审核未通过'.$info['works_reason'];
        }
        $list = TechnicianQualifications::alias('a')
                ->where(['a.technician_id'=>$t_id])
                ->leftjoin('job b','b.id=a.job_id')
                ->field('a.id,b.job_name,a.status,a.check_reason')
                ->all()->toArray();
        if(!$list){
            $arr[] = '资质认证未提交';
        }else{
            foreach ($list as $key => $value) {
                if($value['status'] == 2){
                    $arr[] = '资质认证-'.$value['job_name'].'未通过:'.$value['check_reason'];
                }
            }
        }
        return $arr;
    }
}